<template>
    <h2>{{ $t('user.change_pwd.title') }}</h2>
    <el-form :model="form" label-position="top" :rules="rules" ref="formRef">
        <el-form-item :label="$t('user.change_pwd.account')" prop="account">
            <el-input v-model="form.account" disabled />
            <p>{{ $t('user.change_pwd.password_rule') }}</p>
        </el-form-item>
        <el-form-item :label="$t('user.change_pwd.old_password')" prop="old_password" required>
            <el-input v-model="form.old_password" type="password" show-password />
        </el-form-item>
        <el-form-item :label="$t('user.change_pwd.password')" prop="password" required>
            <el-input v-model="form.password" type="password" show-password />
        </el-form-item>
        <el-form-item :label="$t('user.change_pwd.confirm_password')" prop="confirm_password" required>
            <el-input v-model="form.confirm_password" type="password" show-password />
        </el-form-item>
        <div class="btn-list">
            <el-button @click="submitHandle" color="#F06423" :loading="loading">{{ $t('golbal.btn_text_1') }}</el-button>
            <el-button @click="cancleHandle" color="#F06423" :disabled="loading">{{ $t('golbal.btn_text_3') }}</el-button>
        </div>
    </el-form>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'

import { useUserStore } from '@/stores/user'
import AuthService from '@/api/auth'
import { useAuthStore } from '@/stores/auth'

import { useI18n } from 'vue-i18n'
import type { SupportedLocales } from '@/locales/i18n'
import type { MessageSchema } from '@/types/i18n'

const router = useRouter()
const userStore = useUserStore()
const authStore = useAuthStore()
const loading = ref(false)
const { t } = useI18n<{ message: MessageSchema }, SupportedLocales>()

const formRef = ref()
const form = ref({
    id: userStore.userinfoL?.id || 0,
    account: userStore.userinfoL?.account,
    old_password: '',
    password: '',
    confirm_password: '',
})
const rules = ref({
    old_password: [{ required: true, message: t('user.change_pwd.validate.old_password'), trigger: 'change' }],
    confirm_password: [{ required: true, message: t('user.change_pwd.validate.confirm_password'), trigger: 'change' }],
    confirm_password_agin: [
        { required: true, message: t('user.change_pwd.validate.confirm_password_again'), trigger: 'change' },
        { validator: validateConfirmPassword, trigger: 'blur' },
    ],
})
function validateConfirmPassword(rule: any, value: any, callback: any) {
    if (value !== form.value.confirm_password) {
        callback(new Error(t('user.change_pwd.validate.not_match')))
    } else {
        callback()
    }
}
const submitHandle = () => {
    loading.value = true
    formRef.value.validate((valid: any) => {
        if (valid) {
            const { id, old_password, password, confirm_password } = form.value
            AuthService.updatePassword({ id, old_password, password, confirm_password })
                .then(() => {
                    authStore.clearToken()
                    router.push('/')
                })
                .finally(() => {
                    loading.value = false
                })
        } else {
            loading.value = false
        }
    })
}
const cancleHandle = () => {
    router.go(-1)
}

watch(
    () => userStore.userinfoL,
    () => {
        if (!userStore.userinfoL) return
        form.value.id = userStore.userinfoL.id
        form.value.account = userStore.userinfoL.account
    },
)
</script>

<style lang="less" scoped>
h2 {
    margin-bottom: 30px;
    font-size: 30px;
    font-weight: normal;
    line-height: 1.2;
    color: #000;
}

:deep(.el-form) {
    .el-form-item {
        .el-form-item__label {
            color: #4c4c4c;
        }
    }

    p {
        font-size: 12px;
        color: #4c4c4c;
    }

    .btn-list {
        display: flex;
        justify-content: center;
    }
}
</style>
